package com.ikkong.myproject.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.ikkong.checks.model.Checks;
import com.ikkong.checks.service.ChecksService;
import com.ikkong.checks.service.impl.ChecksServiceImpl;
import com.ikkong.cloud.model.Cloud;
import com.ikkong.cloud.service.CloudService;
import com.ikkong.cloud.service.impl.CloudServiceImpl;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.core.toolbox.support.BladePage;
import com.ikkong.projects.model.Projects;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.myproject.model.Myproject;
import com.ikkong.myproject.service.MyprojectService;
import com.ikkong.myproject.service.impl.MyprojectServiceImpl;
import com.ikkong.system.model.Dept;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.upload.UploadFile;
import org.beetl.sql.core.SQLScript;

/**
 * Generated by Blade.
 * 2018-03-31 21:30:59
 */
public class MyprojectController extends UrlPermissController {
	private static String CODE = "myproject";
	private static String PERFIX = "projects";
	private static String LIST_SOURCE = "Myproject.list";
	private static String CHECKS_SOURCE = "Myproject.checks";
	private static String MYLIST_SOURCE = "Myproject.mylist";
	private static String BASE_PATH = "/platform/myproject/";
	public static String FILE_NAMES = "";
	
	MyprojectService service = new MyprojectServiceImpl();
    ChecksService cs = new ChecksServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "myproject.html");
	}

	public void stageaccept() {
		setAttr("code", CODE);
		render(BASE_PATH + "stageaccept.html");
	}

	public void checks(){
		setAttr("code", CODE);
		render(BASE_PATH + "checks.html");
	}
	public void checksover(){
        Integer id = getParaToInt(0);
        Myproject pro = service.findById(id);
        Integer dept_id = pro.getDept_id();
        Dept dept = Blade.create(Dept.class).findById(dept_id);
        if(dept != null){
            setAttr("dept",dept.getFullname());
        }
        setAttr("pro",pro);
        setAttr("code", CODE);
		render(BASE_PATH + "checksover.html");
	}
	public void oversave(){
		renderText("验收完成");
	}
    public void checkssave(){

        Checks bean = getBean(Checks.class);
		Integer p_id = bean.getP_id();
		Myproject myproject = new Myproject();
		myproject.setId(p_id);
		myproject.setStatus(17);
		boolean update = service.update(myproject);
		boolean temp = cs.save(bean);
        if (temp && update) {
            setAttr("href","/listcheck/index");
            setAttr("msg","即将跳转到验收列表");
            renderJson(success(SAVE_SUCCESS_MSG));
        } else {
            renderJson(error(SAVE_FAIL_MSG));
        }
    }

	public void checksadd(){
        Integer id = getParaToInt(0);
        Myproject pro = service.findById(id);


        Dept dept = Blade.create(Dept.class).findById(pro.getDept_id());

        setAttr("pro",pro);
        setAttr("dept",dept.getFullname());
        setAttr("code", CODE);
		render(BASE_PATH + "../checks/checks_add.html");
	}
	public void add() {
		Date d = new Date();
		Integer year = com.ikkong.system.util.DateUtil.getYear(d);
		int year1 = year + 1;
		setAttr("year",year);
		setAttr("yea",year1);
		FILE_NAMES = "";
		setAttr("code", CODE);
		String deptName = ShiroKit.getUser().getDeptName().toString();
		String deptId = ShiroKit.getUser().getDeptId().toString();
		setAttr("deptId",deptId);
		setAttr("deptName",deptName);
		render("/platform/projects/projects_add.html");
	}

    public void add2() {
        Date d = new Date();
        Integer year = com.ikkong.system.util.DateUtil.getYear(d);
        int year1 = year + 1;
        setAttr("year",year);
        setAttr("yea",year1);
        FILE_NAMES = "";
        setAttr("code", CODE);
        String deptName = ShiroKit.getUser().getDeptName().toString();
        String deptId = ShiroKit.getUser().getDeptId().toString();
        setAttr("deptId",deptId);
        setAttr("deptName",deptName);
        render("/platform/projects/projects_add2.html");
    }

	/*
	统一ajax文件上传路径,返回文件名
	 */
	public void view(){
		String fileName = "";
		String extName = "";
		boolean flag = false;
		String msg = "";
		try {
			String m = "1";
			List<UploadFile> uf = getFiles();
			for(int i = 0;i<uf.size();i++){
				String n = uf.get(i).getFileName().toString();
				FILE_NAMES = n + "," +  FILE_NAMES;
				m = n + "||" + m;
			}
			File file = uf.get(0).getFile();
			fileName = file.getName();
			flag = true;
			msg = "上传成功";
		}
		catch (Exception e) {
			msg = "网络超时，请重试。";
		}
		JSONObject json = new JSONObject();

		json.put("flag", flag);
		json.put("msg", msg);
		json.put("fileName", fileName);
		json.put("names",FILE_NAMES);
		renderJson(json);
	}

	public void edit() {
		FILE_NAMES = "";
		String id = getPara(0);
		Myproject myproject = service.findById(id);
		setAttr("projects",myproject);
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "myproject_edit.html");
	}


	public void list() {
//        int page = getParaToInt("page");
//        int rows = getParaToInt("rows");
//		int cat = getParaToInt("cat");
//        int p = (page-1)*rows;
        String deptid = ShiroKit.getUser().getDeptId().toString();
        List<Map> mylist = Blade.dao().select("Myproject.mylist", Map.class, Record.create().set("deptid", deptid));
        renderJson(mylist);
	}
	public void setss() {
		String deptid = ShiroKit.getUser().getDeptId().toString();
		List<Map> mylist = Blade.dao().select("Myproject.settingup", Map.class, Record.create().set("deptid", deptid));
		renderJson(mylist);
	}
	public void checkslist() {
		Object grid = paginate(CHECKS_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Myproject myproject = mapping(PERFIX, Myproject.class);
		boolean temp = service.save(myproject);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Myproject myproject = mapping(PERFIX, Myproject.class);
        Myproject pro = service.findById(myproject.getId());
        Integer status = pro.getStatus();
        if(status==1){
            myproject.setStatus(0);
        }
        boolean temp = service.update(myproject);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}
    public void download(){
        Integer id = getParaToInt("id");
        Myproject pro = service.findById(id);
        if(pro !=null){
			if(pro.getStatus() == 3){
				if(pro.getPlan_file()!=null ||pro.getPlan_file()!=""){
					String annex = pro.getPlan_file();
					renderFile(annex);
				}
			}else if(pro.getStatus() == 13){
				if(pro.getPlan_file()!=null ||pro.getPlan_file()!=""){
					String zbfile = pro.getZb_file();
					renderFile(zbfile);
				}
			}

        }else {
				renderText("没有找到此文件");
			}


    }
	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
